package org.seedltd.upms.service.impl.detection;

import java.util.Date;
import java.util.List;

import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.StringUtils;
import org.seedltd.springsecurity.utils.UserUtils;
import org.seedltd.upms.constant.DictKeyConst;
import org.seedltd.upms.mapper.detection.DetectionOrderTracingMapper;
import org.seedltd.upms.model.detection.entity.DetectionOrderTracing;
import org.seedltd.upms.model.detection.vo.DetectionOrderTracingVO;
import org.seedltd.upms.model.sys.vo.DictEnumVO;
import org.seedltd.upms.service.detection.DetectionOrderTracingService;
import org.seedltd.upms.service.sys.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;

/**
 * 订单状态节点业务实现
 * 
 * @author laizuan
 * @since 2021/08/02
 */
@Service
@AllArgsConstructor
public class DetectionOrderTracingServiceImpl extends
    BaseServiceImpl<DetectionOrderTracingMapper, DetectionOrderTracing> implements DetectionOrderTracingService {

    private final SysDictService sysDictService;

    @Override
    public void addByOrderId(Long orderId, String defaultStatus) {
        List<DictEnumVO> simple = sysDictService.listSimpleByType(DictKeyConst.DETECTION_ORDER_STATUS);
        List<DetectionOrderTracing> tracingList = Lists.newArrayListWithExpectedSize(simple.size());
        DetectionOrderTracing tracing = null;
        String nickName = UserUtils.getNickName();
        int i = 0;
        for (DictEnumVO dictEnumVO : simple) {
            tracing = new DetectionOrderTracing();
            tracing.setOrderId(orderId);
            tracing.setFlowCode(dictEnumVO.getValue());
            tracing.setFlowDesc(dictEnumVO.getDesc());
            if (StringUtils.equals(defaultStatus, tracing.getFlowCode())) {
                tracing.setFlowTime(new Date());
            }
            tracing.setOperator(nickName);
            tracing.setSortNo(++i);
            tracingList.add(tracing);
        }
        this.saveBatch(tracingList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTracing(Long orderId, Integer flowCode, String operatorUser, Date flowTime,
        boolean updateBefore, String remark) {
        LambdaUpdateWrapper<DetectionOrderTracing> updateWrapper = Wrappers.lambdaUpdate(DetectionOrderTracing.class);
        updateWrapper.eq(DetectionOrderTracing::getOrderId, orderId);
        updateWrapper.eq(DetectionOrderTracing::getFlowCode, flowCode);
        updateWrapper.set(DetectionOrderTracing::getOperator, operatorUser);
        updateWrapper.set(DetectionOrderTracing::getFlowTime, flowTime);
        updateWrapper.set(StringUtils.isNotBlank(remark), DetectionOrderTracing::getRemark,
            StringUtils.substring(remark, 0, 200));
        if (updateBefore) {
            baseMapper.updateAfter(orderId, Math.abs(flowCode));
        }
        return this.update(updateWrapper);
    }

    @Override
    public boolean delete(Long orderId) {
        return u().eq(DetectionOrderTracing::getOrderId, orderId).delete();
    }

    @Override
    public List<DetectionOrderTracingVO> listByOrderId(Long orderId) {
        return q().eq(DetectionOrderTracing::getOrderId, orderId).orderByAsc(DetectionOrderTracing::getSortNo)
            .selectList(DetectionOrderTracingVO.class);
    }
}